home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pi-5ways.zip / CANNON.C < prev    next >
C/C++ Source or Header  |  1991-06-15  |  3KB  |  112 lines

  1. /**************************************************************************/
  2. /*     Calculate π by "shooting a cannon" at a round pond of radius 1     */
  3. /*                'inscribed' in a 2 x 2 square of land.                  */
  4. /*     This is a variation of the "Monte Carlo" method of simulation      */
  5. /*                                                                        */
  6. /*       Note|  Center of circle is at (1,1).  Corners of land are:       */
  7. /*                    (0,0), (1,0), (1,1), (0,1).                         */
  8. /*                                                                        */
  9. /*                              Mendel Cooper                             */
  10. /*                             3138 Foster Ave.                           */
  11. /*                            Baltimore, MD 21224                         */
  12. /*                                                                        */
  13. /*                                   06/91                                */
  14. /*                  Source code placed in the public domain               */
  15. /**************************************************************************/
  16.  
  17.  
  18.  
  19.  
  20. /* May need #include <math.h> */
  21.  
  22.  
  23. /*********************************************************************/
  24. /*                            GET_SEED                               */  
  25. /*     Returns 4-digit random seed from BIOS time (up to 5959)       */
  26. /*********************************************************************/
  27.  
  28. int get_seed()
  29.  
  30. {
  31. char  *time, *gettime(),time$[5];
  32. int j;
  33.  
  34. time = gettime();
  35.  
  36. *(time$) = *(time + 3);
  37. *(time$ + 1) = *(time + 4);
  38. *(time$ + 2) = *(time + 6);
  39. *(time$ + 3) = *(time + 7);
  40. *(time$ + 4) = '\0';
  41.  
  42.      j = atoi(time$);
  43.  
  44.      return (j);
  45.  
  46. }
  47.  
  48. /**********************************************************************/
  49. /*                           GETRAND()                                */
  50. /*               Outputs random number (0.0 - 2.0)                    */
  51. /**********************************************************************/
  52.  
  53. double getrand()
  54.  
  55. {
  56. int random;   /*number generated by rand() function*/
  57. double scale_factor = 16384.0;   /*scaling factor*/
  58. double rand0_2;    /*random number between 0.0 and 2.0 returned by fn*/
  59.  
  60. srand(get_seed());  /*seeds random generator*/
  61.  
  62. random = rand();
  63.  
  64. rand0_2 = (double)random / scale_factor;
  65.  
  66. return (rand0_2);
  67. }
  68.  
  69. /**************************************************************************/
  70.  
  71.  
  72. main()
  73. {
  74. double fabs(), sqrt(), getrand(), X, Y, Dx, Dy, distance, ratio, Pi;
  75. int shots = 0, splashes = 0, thuds = 0;
  76.  
  77.  
  78. clrscrn();      /* Clears screen. You may delete this line if error. */
  79.  
  80.  
  81. while (shots < 32000) {      /*integer value, still...*/
  82.  
  83. shots++;
  84.  
  85. X =  getrand();   /*Generate X and Y coordinates*/
  86. Y =  getrand();   /* of shots */
  87. /* Origin is at (1.0,1.0), remember */
  88.  
  89. Dx = fabs( 1 - X);   /*find distance from origin*/   
  90. Dy = fabs( 1 - Y);
  91. distance = sqrt( Dx*Dx + Dy*Dy);  /* by Pythagorean Theorem */
  92.  
  93.  
  94.  
  95.  
  96.  
  97. if (distance <= 1.0)
  98.    { splashes++; printf("SPLASH!     "); }
  99.       else { thuds++; printf("THUD.       "); }
  100.  
  101. printf("%5d splashes | %5d thuds    ", splashes, thuds);
  102.  
  103. ratio = (double)splashes / (double)shots;  /* Should be ≈ ¼π */
  104. Pi = ratio * 4.0;
  105.  
  106. printf("At %5d shots  ---- π ≈ %f. \n", shots, Pi);
  107.  
  108. }
  109.  
  110.  
  111. }
  112.